const app=getApp();
const imgBaseUrl=app.globalData.imgBaseUrl;
const imgPreUrl=app.globalData.imgPreUrl;
const baseUrl=app.globalData.baseUrl;
var WxParse = require('../wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgBaseUrl:imgBaseUrl,
    imgPreUrl:imgPreUrl,
    indicatorDots: true,
    circular: true,
    autoplay: true,
    interval: 3000,
    duration: 1000,
    bannerList:[],
    goodsDetail:"",
    goodsList: [],
    product:{
      name: "****",
      price: "**",
      des: '***'
    }
  },
  onLoad: function (options) {
    wx.setTabBarBadge({
      index: 2,
      text: '999',
    })
    var that=this;
    this.setData({
      isLogin: app.isLogin()
    })
    wx.showLoading({
      title: '数据加载中...',
    })
    app.func.postReq('/text/index.php/home/product/pdetails',{id:options.id,goods_type:options.goods_type},function(res){
      var content="";
      if (res.product.content) {
        content=res.product.content.replace(/<img [^>]*src=['"]([^'"]+)[^>]*>/gi, function (match, capture) {
          if (capture) {
            var http = capture.indexOf('http');
            if (http == -1) {
              var newStr = match.replace(new RegExp(capture, 'g'), baseUrl + capture);
              return newStr;
            } else {
              return match;
            }
          }          
        });
        WxParse.wxParse('goodsDetail', 'html', content, that, 0);
      }
      wx.setNavigationBarTitle({
        title: res.product.name,
      })
      that.setData({
        product:res.product,
        goodsList: res.tj_pros,
        bannerList:res.plunbo,
        web:res.web
      })
    })

    app.func.postReq('/text/index.php/home/shopcar/cars', {}, function (res) {
      that.setData({
        cartNum:res
      })
    })   
  },
  onReady:function(){
    var timer=setTimeout(function(){
      wx.hideLoading();
    },1000);
  },
  prevent: function () {
    //阻止冒泡
  },
  getUserInfo: function (e) {
    var that = this;
    var data = {};
    if (e.detail.errMsg == "getUserInfo:ok") {
      that.login(e.detail.userInfo, e);
    }
  },
  login: function (data, e) {
    var that = this;
    wx.login({
      success: res => {
        data.code = res.code;
        console.log(data);
        app.func.postReq('/text/index.php/home/login/adduser', data, function (res) {
          if (res) {
            wx.setStorageSync('hhh', res.users.id)
            that.setData({ isLogin: 1 })
            that.addCart(e);
          }
        })
      }
    })
  },
  addCart: function (e) {
    const that = this;
    var id = e.currentTarget.dataset.id;
    var cartNum = that.data.cartNum;
    
    cartNum += 1;
    that.setData({
      cartNum: cartNum
    })
    app.func.postReq('/text/index.php/home/shopcar/addscar', { pid: id, num: 1,goods_type:'' }, function (res) {
      if(parseFloat(res)>0){
        wx.showToast({
          title:'添加成功！',
          icon:'none'
        })
      }else if(parseFloat(res) == -1){
        wx.showToast({
          title: '添加失败！',
          icon: 'none'
        })
      }
    })
  },
  onShareAppMessage: function () {
  
  }
})